package org.dant.ant.extension.tasks.internal;

import java.io.*;
import java.util.*;

import org.dant.ant.extension.utils.StringUtils;

public class RemoteResponse implements Serializable {

  public static final int TASK = 0; //startcxxserver tag
  public static final int TESTCASE = 1; //testcase tag
  public static final int TESTSUITE = 2; //testsuite tag

  private boolean success = true;
  private String result;

  private StringBuffer stdOutBuffer;
  private StringBuffer stdErrBuffer;

  private String fullurl;

  private Calendar startTime = Calendar.getInstance();
  private Calendar endTime = Calendar.getInstance();

  private String taskName;
  private String packageName;
  private String name;
  
  private int tagType;
  private String commandType;
  private String command;
  private int duplicate;
  private boolean multsited = false;

  public RemoteResponse() {
    stdOutBuffer = new StringBuffer();
    stdErrBuffer = new StringBuffer();
  }

  public RemoteResponse(String name, String packageName, int type, boolean success) {
    this();
    this.name = name;
    this.packageName = packageName;
    this.tagType = type;
    this.success = success;
  }

  public void setMultiSited(boolean multsited) {
    this.multsited = multsited;
  }

  public boolean getMultiSited() {
    return this.multsited;
  }
  
  public void setName(String name) {
    this.name = name;
  }

  public String getName() {
    return this.name;
  }

  public void setPackageName(String packageName) {
    this.packageName = packageName;
  }
  
  public String getPackageName() {
    return this.packageName;
  }


  public void setTaskName(String taskName) {
    this.taskName = taskName;
  }

  public String getTaskName() {
    return this.taskName;
  }

  public int getTagType() {
    return this.tagType;
  }
  
  public void setTagType(int type) {
    this.tagType = type;
  }

  public void setStartTime(long startTime) {
    this.startTime.setTimeInMillis(startTime);
  }
  
  public void setEndTime(long endTime) {
    this.endTime.setTimeInMillis(endTime);
  }
  
  public long getStartTime() {
    return this.startTime.getTimeInMillis();
  }

  public long getEndTime() {
    return this.endTime.getTimeInMillis();
  }

  public long getTimes() {
    return endTime.getTimeInMillis() - startTime.getTimeInMillis();
  }

  public void setTimes(long times) {
  }
  
  public void setFullURL(String localhost) {
    this.fullurl = localhost;
  }

  public String getFullURL() {
    return this.fullurl;
  }

  public void setDuplicate(int duplicate) {
    this.duplicate = duplicate;
  }

  public int getDuplicate() {
    return this.duplicate;
  }
  
  public void setCommand(String command) {
    this.command = command;
  }

  public String getCommand() {
    return this.command;
  }

  public void setCommandType(String cmdType) {
    this.commandType = cmdType;
  }

  public String getCommandType() {
    return this.commandType;
  }

  public boolean isSuccess() {
    return success;
  }
  
  public void setSuccess(boolean val) {
    this.success = val;
  }

  public void setResult(String val) {
    this.result = val;
  }
  
  public String getResult() {
    if (result == null) return "N/A";
    return result;
  }

  public void appendResult(Object val) {
    if (val == null) return;
    StringBuffer sb = new StringBuffer();
    if (result != null) sb.append(result.toString());
    sb.append("\n\n");
    sb.append(val.toString());
    setResult(sb.toString());
  }

  public void setStdOutBuffer(StringBuffer sb) {
    appendStdOutBuffer(sb.toString());
  }
  
  public void setStdErrBuffer(StringBuffer sb) {
    appendStdErrBuffer(sb.toString());
  }

  public void appendStdOutBuffer(String stdout) {
    this.stdOutBuffer.append(stdout);
    this.stdOutBuffer.append("\n\n");
  }

  public void appendStdErrBuffer(String stderr) {
    this.stdErrBuffer.append(stderr);
    this.stdErrBuffer.append("\n\n");
  }

  public StringBuffer getStdOutBuffer() {
    return this.stdOutBuffer;
  }

  public StringBuffer getStdErrBuffer() {
    return this.stdErrBuffer;
  }

  public String toString() {
    StringBuffer buffer = new StringBuffer();
    
    if (fullurl != null) {
      buffer.append("\n-----------------Results---------------");
      buffer.append("\nFrom: ");
      buffer.append(fullurl);
    }
    
    buffer.append("\nStarts at:");
    buffer.append(this.startTime.getTime().toString());
    
    buffer.append("\nTask: " );
    buffer.append(this.taskName);

    buffer.append("\nCommand Type: ");
    buffer.append(commandType);

    if (command != null) {
      buffer.append("\nCommand: ");
      buffer.append(command);
    }

    buffer.append("\nsuccess : " + success);
    if (result != null) {
      buffer.append("\nresult : \n" + result.toString());
    }
    if (stdOutBuffer != null) {
      buffer.append("\nstdout : \n" + stdOutBuffer);
    }
    if (stdErrBuffer != null) {
      buffer.append("\nstderr : \n" + stdErrBuffer);
    }
    
    buffer.append("\nEnds at: " );
    buffer.append(this.endTime.getTime().toString());
    buffer.append("\n---------------------------------------\n");
    
    return buffer.toString();
  }
}
